package com.hdoit.ares.web.ser.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.ser.model.GoodStore;
import com.hdoit.ares.logical.ser.model.GoodsService;
import com.hdoit.ares.logical.ser.service.IGoodStoreService;
import com.hdoit.ares.logical.ser.service.IGoodsServiceService;
import com.hdoit.ares.logical.ser.service.IStaffService;
import com.hdoit.ares.logical.viewmodel.ser.GoodsServiceSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNoInteger;
import com.tennetcn.common.logical.superbase.service.IRoleEmployeeService;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.ChineseUtils;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author      lvsi
 * @email       
 * @createtime  2017年11月22日 14:49:43
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/ser/goodsService/")
public class GoodsServiceController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/ser/goodsService/";
    }

    private IGoodsServiceService goodsServiceService;
    public IGoodsServiceService getGoodsServiceService() {
        return goodsServiceService;
    }
    @Resource
    public void setGoodsServiceService(IGoodsServiceService goodsServiceService) {
        this.goodsServiceService = goodsServiceService;
    }
    
    private IGoodStoreService goodStoreService;

    public IGoodStoreService getGoodStoreService() {
		return goodStoreService;
	}
    @Resource
	public void setGoodStoreService(IGoodStoreService goodStoreService) {
		this.goodStoreService = goodStoreService;
	}
    private IStaffService staffService;

	public IStaffService getStaffService() {
		return staffService;
	}
	@Resource
	public void setStaffService(IStaffService staffService) {
		this.staffService = staffService;
	}

   	private IRoleEmployeeService roleEmployeeService;

    public IRoleEmployeeService getRoleEmployeeService() {
		return roleEmployeeService;
	}
    @Resource
	public void setRoleEmployeeService(IRoleEmployeeService roleEmployeeService) {
		this.roleEmployeeService = roleEmployeeService;
	}
    
	private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="goodsServiceDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
    private final String getPhoneByStoreIdViewName="getPhoneByStoreId";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,GoodsServiceSearch goodsServiceSearch,boolean ajaxPost){

    	String loginId=getLoginModel().getLoginId();
    	boolean result=roleEmployeeService.queryCountRolesByEmployeeId("超级管理员",loginId);
    	List<GoodsService> goodsServices=null;
    	if(goodStoreService.queryListStoreById(loginId).size()>0){
    		/*该用户为店主*/
    		 pagerModel.setTotalAmount(goodsServiceService.queryGoodsServiceCountById(goodsServiceSearch,loginId));
    	     goodsServices= goodsServiceService.queryGoodsServiceListById(goodsServiceSearch,pagerModel,loginId);
    	}else{
    		if(staffService.queryStaffById(loginId).size()>0){
    			/*该用户为店铺员工*/
    			pagerModel.setTotalAmount(goodsServiceService.queryStoreGoodsCountById(goodsServiceSearch,loginId));
    	        goodsServices= goodsServiceService.queryStoreGoodsById(goodsServiceSearch,pagerModel,loginId);
    		}else if(result==true){
    			/*该用户为管理员*/
    			pagerModel.setTotalAmount(goodsServiceService.queryGoodsServiceCount(goodsServiceSearch));
                goodsServices= goodsServiceService.queryGoodsServiceList(goodsServiceSearch,pagerModel);
    		}
    	}

        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("goodsServices", goodsServices);
        model.addAttribute("goodsServiceSearch", goodsServiceSearch);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String goodsServiceDetailed(Model model,String key){
        GoodsService goodsService=new GoodsService();
        if(!StringUtils.isEmpty(key)){
            goodsService=goodsServiceService.queryGoodsServiceModel(key);
        }
        model.addAttribute("goodsService", goodsService);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,GoodsService goodsService){
        if(StringUtils.isEmpty(goodsService.getId())){
            goodsService.setId(CommonUtils.getKeyId());    
            if(StringUtils.isEmpty(goodsServiceService.queryMaxGoodsSortCode())){
            	goodsService.setGoodsSortCode(0);
            }else{
            	goodsService.setGoodsSortCode(goodsServiceService.queryMaxGoodsSortCode()+5);
            }
            goodsService.setCreateDate(DateUtils.getCurrent());
            goodsService.setDeleteMark(YesOrNoInteger.no);
            goodsService.setCreateName(getLoginModel().getLoginName());
            goodsService.setModelStatus(ModelStatus.add);
        }else{
            goodsService.setModifyDate(DateUtils.getCurrent());
            goodsService.setModifyName(getLoginModel().getLoginName());
            goodsService.setModelStatus(ModelStatus.update);
        }
        
        goodsService.setCode(ChineseUtils.getPingYin(goodsService.getServiceGoodsName()));
        goodsService.setMarkCode(ChineseUtils.getPinYinHeadChar(goodsService.getServiceGoodsName()));

        ActionResult actionResult;
        if(goodsServiceService.applyChange(goodsService)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("goodsService", goodsService);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }
    
    @RequestMapping(value = getPhoneByStoreIdViewName, method = { RequestMethod.GET,RequestMethod.POST},produces="text/plain;charset=utf-8")
   	@ResponseBody
   	public String getPhoneByStoreId(String storeId){

       	GoodStore goodStore=goodStoreService.queryPhoneByStoreId(storeId);
       	if(!StringUtils.isEmpty(goodStore.getStorePhone())){
       		String phone=goodStore.getStorePhone();
       		return phone;
       	}else{
       		return "1";
       	}    
       }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(goodsServiceService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
    
}
